function timechange() {
	this.distance = 0;
	this.day = 0;
	this.hour = 0;
	this.minute = 0;
	this.second = 0;
}

timechange.prototype.setDistance = function(dis) {
	this.distance = dis;
};

timechange.prototype.getDistance = function() {
	return this.distance;
};

// 根据获取的参数来展现界面天，小时，分，秒的数据
timechange.prototype.showDistance = function() {
	this.dealDistance();

	var showday = document.all("day");
	if (showday != null)
		var showhour = document.all("hour");
	var showminute = document.all("minute");
	var showsecond = document.all("second");

	if (typeof (showday) == "object") {
		showday.innerHTML = this.day;
	}

	if (typeof (showhour) == "object") {
		showhour.innerHTML = this.hour;
	}

	if (typeof (showminute) == "object") {
		showminute.innerHTML = this.minute;
	}

	if (typeof (showsecond) == "object") {
		showsecond.innerHTML = this.second;
	}
};

// 根据获取的参数来展现界面天，小时，分，秒的数据
timechange.prototype.dealDistance = function() {
	var tempdis = parseInt(this.distance);
	if (tempdis >= 0) {
		this.day = parseInt(this.distance / (24 * 60 * 60));
		var tempdata = this.distance % (24 * 60 * 60);
		this.hour = parseInt(tempdata / (3600));
		var minre = parseInt(tempdata % (3600));
		this.minute = parseInt(minre / 60);
		this.second = parseInt(minre % 60);
	}
};

// 每秒钟处理一次distance
timechange.prototype.minusDistance = function() {
	if (this.distance > 0) {
		this.distance = parseInt(this.distance) - 1;
	}
};

// 添加以1秒执行一次的方法
function wakeInterval() {
	change.showDistance();
	if (change.distance > 0) {
		window.setTimeout(wakeInterval, 1000);
	}
	change.minusDistance();

}
// 删除左右两端的空格
function trim(str) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}
// 删除左边的空格
function ltrim(str) {
	return str.replace(/(^\s*)/g, "");
}
// 删除右边的空格
function rtrim(str) {
	return str.replace(/(\s*$)/g, "");
}

function round(v, e) {
	var t = 1;
	for (; e > 0; t *= 10, e--)
		;
	for (; e < 0; t /= 10, e++)
		;
	return Math.round(v * t) / t;
}

// 自动计算报价信息
function calPriceAndTotal(index) {
	// 数量
	var numberobj = document.all("number" + index);
	// 目录单价
	var priceobj = document.all("price" + index);
	// 正折扣率
	var discountobj = document.all("discount" + index);
	// 折扣后单价
	var unitpriceobj = document.all("unitprice" + index);
	// 折扣后总价
	var amountobj = document.all("amount" + index);
	var number;
	var price;
	var discount;
	// 控制数量留两位小数
	if (numberobj != null && numberobj != null) {
		number = trim(numberobj.value);
		if (number != null && number != '') {
			number = round(number, 2);
			numberobj.value = number;
		}
	}
	// 控制单价数据留两位小数
	if (priceobj != null && priceobj != null) {
		price = trim(priceobj.value);
		if (price != null && price != '') {
			price = round(price, 2);
			priceobj.value = price;
		}
	}
	// 控制折扣率保留两位小数
	if (discountobj != null && discountobj != null) {
		discount = trim(discountobj.value);
		if (discount != null && discount != '') {
			discount = round(discount, 2);
			discountobj.value = discount;
		}
	}

	if (priceobj != null && discountobj != null) {
		discount = discountobj.value;
		price = priceobj.value;
		discount = discountobj.value;
		if (price != '' && discount != '' && price != null && price != 'null') {
			// 折扣后单价
			var unitprice = (parseFloat(price) * (parseFloat(discount) / 100));
			// var unitprice = ajaxToolUtils.mutiDouble(price,
			// parseFloat(discount) / 100);
			unitprice = round(unitprice, 2);
			// 折扣后单价字段赋值
			unitpriceobj.value = unitprice;
			// 数量不为空
			number = numberobj.value;
			if (number != null && number != '') {
				// 计算折扣后总价
				var tempvalue = parseFloat(unitprice) * parseFloat(number);
				tempvalue = round(tempvalue, 2);
				amountobj.value = tempvalue;
			}
			// 如果目录单价为空，折扣单价，折扣总价等不做计算同样为空
		} else {
			unitpriceobj.value = '';
			amountobj.value = '';
		}
	}
	calTotalMoney();
}

function calTotalMoney() {
	// 折扣后总价
	var amountobj = document.getElementsByName("discounted_amount");
	if (amountobj.length > 0) {
		var totalMoney = 0;
		for ( var i = 0; i < amountobj.length; i++) {
			if (amountobj[i].value != null && amountobj[i].value != "") {
				totalMoney = parseFloat(totalMoney)
						+ parseFloat(amountobj[i].value);
			}
		}
		document.getElementById("totalMoney").value = totalMoney;
	}

}
function clearNoNum(obj) {
	// 先把非数字的都替换掉，除了数字和.
	// 先把非数字的都替换掉，除了数字和.
	obj.value = obj.value.replace(/[^\d.]/g, "");
	// 必须保证第一个为数字而不是.
	obj.value = obj.value.replace(/^\./g, "");
	// 保证只有出现一个.而没有多个.
	obj.value = obj.value.replace(/\.{2,}/g, ".");
	// 保证.只出现一次，而不能出现两次以上
	obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$",
			".");
	// 小数点后只能输入2位。
	// obj.value = obj.value.match(/^\d*\.?(\d{0,2})?/ig);
}

var isin = false;
function test2(objTR) {
	isin = true;
	if (objTR.lastvalue == null) {
		objTR.lastvalue = "";
	}
	if (objTR.value == "") {
		objTR.lastvalue = "";
	}
	var pattern = /^([0-9.]+)$/;
	if (pattern.test(objTR.value) || objTR.value == "") {
	} else {
		objTR.value = objTR.lastvalue;
	}
	var txtval = objTR.value;
	isin = false;
}
// 只允许录入数据字符 0-9 和小数点
function KeyPress(objTR) {
	txtval = objTR.value;
	var key = event.keyCode;
	if ((key < 48 || key > 57) && key != 46) {
		event.keyCode = 0;
		return;
	} else {
		if (key == 46) {
			if (txtval.indexOf(".") != -1 || txtval.length == 0)
				event.keyCode = 0;
			return;
		}
	}
	objTR.lastvalue = objTR.value;
}

// 定位上传文件的输入位置
function showfiledialog(rowindex) {
	var file = document.all("file" + rowindex);
	var tempx = window.event.x;
	var tempy = window.event.y;

	var imgw = 50;
	var imgh = 20;
	file.style.top = tempy - window.event.offsetY
			+ document.documentElement.scrollTop;
	file.style.left = tempx - window.event.offsetX
			+ document.documentElement.scrollLeft - 12;
}

// 删除上传文件
function deleteRow(index) {
	var root = document.all("fileuploadtable");
	var row = "row" + index;

	var rows = root.rows;
	var deletepos = 0;

	// 寻找到对应的删除行并进行删除
	for ( var pos = 0; pos < rows.length; pos++) {
		var temprow = rows[pos];
		if (temprow.id == row) {
			// 在删除行信息以前更新此行一下行的索引
			deletepos = pos;
			for ( var index = pos + 1; index < rows.length; index++) {
				var temprow = rows[index];
				var cells = temprow.cells;
				var cell = cells[0];
				if (typeof (cell) == 'object') {
					cell.innerText = index;

				}
			}
			root.deleteRow(pos);
			break;
		}
	}
}

/*
 * 当输入文件的信息进行改变后对对应的文件进行签名和加密
 */
function ondatachange(fileobject, index, miWen, ContentMi) {
	// 判断文件对象的值是否为空
	if (fileobject.value != null && fileobject.value != "") {
		// 获取对应文件所在的位置
		var row = document.all("row" + index);
		var cells = row.cells;
		var cell = cells[1];
		// 把文件的路径进行截取显示文件的名称
		cell.innerHTML = fileobject.value.substring(fileobject.value
				.lastIndexOf("\\") + 1, fileobject.value.lastIndexOf("."));
		// 隐藏掉上传文件按钮
		var img = document.all("img" + index);
		img.style.visibility = "hidden";
		// 把加密的文件信息发到对应的位置
		var filecontent = document.all("filecontent" + index);

		if (filecontent != null) {
			filecontent.value = ContentMi;
		}

		// 把签名了文件的信息保存到对应的位置
		var signfile = document.all("signfile" + index);
		if (signfile != null) {
			signfile.value = miWen;
		}
		fileobject.style.display = "none";
	}
}

/*
 * 删除报价信息
 */
function removepricemessage(index) {
	var pricetable = document.all("pricetable");

	if (pricetable != null) {
		// 获取报价信息有多少行
		var rows = pricetable.rows;

		if (rows != null && rows.length > 0) {
			for ( var pos = 0; pos < rows.length; pos++) {
				var row = rows[pos];
				// 寻找要删除的row
				if (row.id == index) {
					// 删除对应的报价信息
					pricetable.deleteRow(pos);
					break;
				}
			}
		}

		// 设置动态头的滚动条的位置
		if (pricetable.rows.length == 0) {
			var movediv = document.all("movediv");
			if (movediv != null) {
				movediv.scrollLeft = 0;
			}
		}
	}

}

// 设置头和表体同时向同一个方向移动 主要是针对报价信息来说
function setScrollData(obj) {
	var movediv = document.all("movediv");
	movediv.scrollLeft = obj.scrollLeft;

}

/**
 * 增加附件行
 * 
 * @param fileStrs
 * @return
 * @创建者: zhenglq
 * @创建时间: 2010-11-16下午06:23:07
 * @简要说明:
 * 
 * --------------------------------------------------------------
 * 
 * @修改者:
 * @修改时间:
 * @修改理由:
 */
function addFileRow(attForms) {
	if (attForms == null) {
		return;
	}
	var index = 0;
	var root = document.all("fileuploadtable");
	var rows = root.rows;
	// 寻找到对应的删除行并进行删除
	var table = document.getElementById("fileuploadtable");
	var tableLength = table.rows.length;
	// 第一列和第二类分别是标题
	for ( var pos = 2; pos < tableLength; pos++) {
		table.deleteRow(2);
	}
	var length = attForms.length;
	for ( var i = 0; i < length; i++) {
		var attForm = attForms[i];
		// 设置行id
		var row = root.insertRow();
		row.style.height = '26px';
		row.id = attForm.pk_da_att;

		// 设置序号
		var cell1 = row.insertCell();
		cell1.align = "center";
		cell1.innerText = attForm.index;

		// 设置附件名称
		var cell2 = row.insertCell();
		cell2.align = "center";
		cell2.innerText = attForm.filename;
		// 设置删除按钮
		var cell3 = row.insertCell();
		cell3.align = "center";
		cell3.innerHTML = "<img src='../images/botton_delete.jpg' border='0' onclick=delFileByFileId('"
				+ attForm.pk_da_att + "') style='cursor: hand' />";
	}
}
function delFileByFileId(pkAtt) {
	if (typeof (change) == "object") {
		if (change != null) {
			if (change.distance <= 0) {
				alert("应答截止时间已过，不能执行此操作");
				return;
			}
		}
	}
	ajaxFileService.delFile(pkAtt, delFileCallBack);
}
/**
 * 
 * 删除附件方法
 * 
 * @param value
 * @return
 * @创建者: zhenglq
 * @创建时间: 2010-11-17上午11:03:45
 * @简要说明:
 */
function delFileCallBack(value) {
	if (value != null && value != "") {
		delLineById(value, "fileuploadtable");
		alert("删除附件成功!");
	} else {
		alert("删除附件失败!");
	}
}
/**
 * 
 * 根据元素Id删除数据
 * 
 * @param index
 * @param tableId
 * @return
 * @创建者: zhenglq
 * @创建时间: 2010-11-17上午11:14:00
 * 
 */
function delLineById(index, tableId) {
	var deltable = document.all(tableId);
	if (deltable != null) {
		// 获取报价信息有多少行
		var rows = deltable.rows;
		if (rows != null && rows.length > 0) {
			for ( var pos = 0; pos < rows.length; pos++) {
				var row = rows[pos];
				// 寻找要删除的row
				if (row.id == index) {
					// 删除对应的报价信息
					deltable.deleteRow(pos);
					break;
				}
			}
		}
	}
}
